#include "main.h"

//LED
uchar led_code = 0;

//SEG
uchar seg_pos = 0;
uchar seg_str[10];
uchar seg_buf[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uchar seg_mode = 0;

//TIMER
ulong t1_count = 0;


//===========TASK=============
void task_proc() {

}


//===========LED=============
void led_proc() {
	if(t1_count%50) return;
	
	//时间界面
	if(seg_mode == 0) led_code |= 0x01;
	else led_code &= ~0x01;
}


//===========SEG=============
void seg_proc() {
	if(t1_count%100) return;
	
	//数据采集
	
	switch(seg_mode) {
		case 0://1 时间
			sprintf(seg_str, "12345678");
		break;
		case 1://1 时间
			sprintf(seg_str, "90ABCDEFOP");
		break;
		case 2://1 时间
			sprintf(seg_str, "HLP--- ---");
		break;
	}
	
	seg_tran(seg_str, seg_buf);
}


//===========KEY=============
void key_proc() {
	static uchar key_val,key_down,key_up,key_old;
	
	if(t1_count%10) return;
	
	//读取键码
	key_val = key_scan_kbd();
	key_down = key_val & (key_old^key_val);
	key_up = ~key_val & (key_old^key_val);
	key_old = key_val;

	//主界面切换
	if(key_down == 4) {
		if(++seg_mode == 2)
			seg_mode = 0;
	}
	
	
}


//===========ISR=============
void timer1_init() {
	AUXR &= 0xBF;
	TMOD &= 0x0F;
	TH1 = 0xFC;
	TL1 = 0x18;
	TF1 = 0;
	ET1 = 1;
	TR1 = 1;
	EA = 1;
}

void isr_timer_1() interrupt 3 {
	t1_count++;
	
	LED(led_code);
	seg_disp(seg_pos, seg_buf);
	if(++seg_pos == 8) seg_pos = 0;
}


//===========MAIN=============
void main() {
	LED(0);
	BUR(0);
	//ne555_init();
	timer1_init();

	//DS1302_setRTC(RTC);
	//DS18B20_readTemp();
	
	while(1) {	
		led_proc();
		seg_proc();
		key_proc();
	}
}


